package com.purong.purong.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 每日上新分类适配器
 */
public abstract class HeadRecycleAdapter<T,VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter{

    public final static int TYPE_HEADER = 0;
    public final static int TYPE_BODY = 1;
    private List<T> recList = new ArrayList<>();
    private View mHeaderView;
    public void addHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public void setData(List jokeList) {
//        if (Utils.isEmpty(imageList)) {
//            return;
//        }
        this.recList = jokeList;

    }
    public List<T> getData() {
//        if (Utils.isEmpty(imageList)) {
//            return;
//        }
        return  this.recList;

    }

    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                      int viewType) {
        //创建circleItem所对应的控件
        View v;
        if (viewType == TYPE_HEADER && mHeaderView != null) {
            v = mHeaderView;
            return new HeadViewHolder(v);
        } else {
           return onCreateView(parent);
        }

    }
    public abstract  RecyclerView.ViewHolder onCreateView(ViewGroup parent);

    public abstract void onBindView(VH mHolder,T data,  int position);
    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder mHolder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) {
            return;
        } else {
            if (mHeaderView != null) {
                position--;
            }
        }
        onBindView((VH)mHolder,recList.get(position),position);
    }

    public int getTotalCount(){
        return recList.size();
    }
    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return mHeaderView != null ? recList.size() + 1 : recList.size();
    }
    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null)
            return TYPE_BODY;
        if (position == 0) {
            return TYPE_HEADER;
        }
        return TYPE_BODY;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager =
                recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager =
                    ((GridLayoutManager) manager);
            gridLayoutManager.setSpanSizeLookup(
                    new GridLayoutManager.SpanSizeLookup() {
                        @Override
                        public int getSpanSize(int position) {          //是HeaderView则占所有列，否则只占自己列
                            return getItemViewType(position) ==
                                    TYPE_HEADER ? gridLayoutManager.getSpanCount() : 1;
                        }
                    });
        }
    }
    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class HeadViewHolder extends RecyclerView.ViewHolder {

        public HeadViewHolder(View v) {
            super(v);
        }
    }
    public boolean includHead(){
        return mHeaderView==null?false:true;
    }
}


